package hust.music.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hust.music.dao.JpaDao;
import hust.music.entity.FriendRel;
import hust.music.entity.Music;
import hust.music.entity.PlayList;
import hust.music.service.IndexModService;
import hust.music.util.DateConvert;
import hust.music.util.DynamicInfo;

/**
 * IndexModServiceImpl
 * 
 * @author Administrator
 * @since 1.0
 */
@Service("indexModService")
@Transactional
public class IndexModServiceImpl implements IndexModService {

	@Autowired
	private JpaDao jpaDao;

	/**
	 * 获取最近好友关系
	 * 
	 * @return 最近好友关系
	 */
	@Override
	public List<DynamicInfo> getRecentFriends(int firstResult, int maxResult) {
		List<FriendRel> friends = jpaDao.findByPage(FriendRel.class,
				"from FriendRel f where f.isPassed = 1 order by f.time desc",
				firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (FriendRel f : friends) {
			DynamicInfo dynamicInfo = new DynamicInfo(f.getFriend().getId(), f
					.getFriend().getNickname(), f.getFriend().getAvatar(), f
					.getUser().getId(), f.getUser().getNickname(), null, null,
					null, null, DateConvert.convertDateToString(f.getTime()),
					null, null);
			list.add(dynamicInfo);
		}

		return list;
	}

	/**
	 * 获取用户上传歌曲
	 * 
	 * @return 用户上传歌曲
	 */
	@Override
	public List<DynamicInfo> getRecentUploadMusic(int firstResult, int maxResult) {
		List<Music> musics = jpaDao.findByPage(Music.class,
				"from Music m order by m.uploadTime desc", firstResult,
				maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (Music m : musics) {
			DynamicInfo dynamicInfo = new DynamicInfo(m.getId(), m.getTitle(),
					m.getImage(), m.getUser().getId(), m.getUser()
							.getNickname(), m.getUser().getAvatar(), null, m
							.getSinger(), m.getDescription(), DateConvert
							.convertDateToString(m.getUploadTime()), m
							.getAlbum(), null);
			list.add(dynamicInfo);
		}

		return list;
	}

	/**
	 * 最新创建流派集
	 * 
	 * @return 用户流派集
	 */
	@Override
	public List<DynamicInfo> getRecentPlayList(int firstResult, int maxResult) {
		List<PlayList> playlists = jpaDao.findByPage(PlayList.class,
				"from PlayList p order by p.time desc", firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (PlayList p : playlists) {
			DynamicInfo dynamicInfo = new DynamicInfo(p.getId(), p.getTitle(),
					p.getAvatar(), p.getUser().getId(), p.getUser()
							.getNickname(), null, null, null, null, null, null,
					null);
			list.add(dynamicInfo);
		}

		return list;
	}

}
